package com.skyworth.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {

	private static final Logger LOG = LoggerFactory.getLogger(Application.class);

	@Autowired
	private Environment env;

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(Application.class, args);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		if (!registry.hasMappingForPattern("/templates/**")) {
			registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");

		}


		String baseUploadDir = env.getProperty("fanfou.paths.uploadedFiles");
		String baseUploadUrl = env.getProperty("fanfou.paths.uploadedUrls");

		if (!registry.hasMappingForPattern(baseUploadUrl + "/**")) {
			registry.addResourceHandler(baseUploadUrl + "/**").addResourceLocations(baseUploadDir);

		}

		super.addResourceHandlers(registry);

	}

}
